package com.pro_one.mobile.controller;

import com.aliyuncs.exceptions.ClientException;
import com.pro_one.constant.MessageConstant;
import com.pro_one.constant.RedisConstant;
import com.pro_one.entity.Result;
import com.pro_one.util.SMSUtils;
import com.pro_one.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

    /**
     * 发送预约验证码
     *
     * @param telephone
     * @return
     */
    @RequestMapping("/sendValidateCode")
    public Result sendValidateCode(String telephone) {
        try {
            String validateCode = ValidateCodeUtils.generateValidateCode(6).toString();
            System.out.println(validateCode);
//            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, validateCode);
            jedisPool.getResource().setex(telephone + RedisConstant.SENDTYPE_ORDER, 5 * 60, validateCode);
            jedisPool.getResource().set(telephone, telephone);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    /**
     * 发送登录验证码
     *
     * @param telephone
     * @return
     */
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone) {
        try {
            String validateCode = ValidateCodeUtils.generateValidateCode(6).toString();
            System.out.println(validateCode);
         //   SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, validateCode);
            jedisPool.getResource().setex(telephone + RedisConstant.SENDTYPE_LOGIN, 5 * 60, validateCode);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
